package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.Storage;
import com.neusoft.ecommerce.service.StorageService;

/**
 * @author 紫煦桉
 */
@RestController
public class StorageController {
    private final StorageService storageService;

    public StorageController(StorageService storageService) {
        this.storageService = storageService;
    }

    @GetMapping("/storage_page")
    public ModelAndView storagePage() {
        return new ModelAndView("supply/storage-list");
    }

    @RequestMapping("/storage/list")
    @ResponseBody
    public LayuiPager<Storage> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<Storage> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = storageService.findTotal(pager);
        List<Storage> list = storageService.findList(pager);
        for (Storage storage : list) {
            System.out.println(storage);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }

    @GetMapping("/addStorage")
    public ModelAndView addStorage() {
        return new ModelAndView("supply/storage-add");
    }

    @PostMapping("/storage/add")
    public Map<String, Object> add(Storage storage) {
        System.out.println(storage);
        Map<String, Object> map = new HashMap<>(16);
        if (storageService.add(storage)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

    @GetMapping("/storage/edit_page")
    public ModelAndView editPage(Storage storage, Model model) {
        System.out.println(storage);
        LayuiPager<Storage> pager = new LayuiPager<>(1,1);
        pager.setCondition(storage);
        Storage message = storageService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("supply/storage-edit");
        model.addAttribute("storage", message);
        return view;
    }

    @PostMapping("/storage/edit")
    public Map<String, Object> edit(Storage storage) {
        System.out.println(storage);
        Map<String, Object> map = new HashMap<>(16);
        if (storageService.edit(storage)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

    @PostMapping("/storage/delete")
    public Map<String, Object> delete(Storage storage) {
        System.out.println(storage);
        Map<String, Object> map = new HashMap<>(16);
        if (storageService.delete(storage)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

}
